<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.text.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"   pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>IPL 5 - RED</title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="content-language" content="" />
    <link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<%@ include file="global.jsp" %>
<%
String resposeText = "";
String errorMesg = (String) session.getAttribute("error");
out.println("<script>alert(\\"+errorMesg+"\\)</script>");
if (session.getAttribute("resposeText") != null) {

	if(null != (String)session.getAttribute("resposeText")){ 
		out.println("<script>alert("+(String)session.getAttribute("resposeText")+")</script>");
    }else{
    	out.println("<script>alert(\"No Mesg\")</script>");
    }
	
	if(("Succesfully Submited").equals((String)session.getAttribute("resposeText"))){ 
		out.println("<script>alert(\"Succesfully Submited\")</script>");
    }else if(("User Not Registered").equals((String)session.getAttribute("resposeText"))){
    	out.println("<script>alert(\"User Not Registered\")</script>");
    }else if(("Entry already made").equals((String)session.getAttribute("resposeText"))){
    	out.println("<script>alert(\"Entry already made\")</script>");
    }else if(("Please enter your name").equals((String)session.getAttribute("resposeText"))){
    	out.println("<script>alert(\"Please enter your name\")</script>");
    }else if(("Exception Occurred").equals((String)session.getAttribute("resposeText"))){
    	out.println("<script>alert(\"Exception Occurred\")</script>");
    }else if(("Unauthorized User").equals((String)session.getAttribute("resposeText"))){
    	out.println("<script>alert(\"Unauthorized User\")</script>");
    }else if(("UserName Not Entered").equals((String)session.getAttribute("resposeText"))){
    	out.println("<script>alert(\"UserName Not Entered\")</script>");
    }else{
    	out.println("<script>alert(\\"+errorMesg+"\\)</script>");
    } 
} 
%>


    <div id="page">
        <div id="header">
            <h1><a href="#">IPL-5  2012</a></h1>
        </div>
        <%@ include file="links.jsp" %>
        <div>
			<%
			if(null!=errorMesg){
				%>
				<div style="color:red;">
				<b><%=errorMesg%></b>
				</div>
				<%
			}
			%>
		</div>
        <div id="main">		              
            <div id="content">	
			<%
				String matchId = (String)session.getAttribute("matchId");
				Map resultMap = new TreeMap();
				try {
					Class.forName(db_driver);
					Connection conn = DriverManager.getConnection(db_url, db_user, db_password);
					Statement stmt = conn.createStatement();
					System.out.print("select winner, mom, sixes, sixesWinner from matchresults where matchId='"+matchId+"'");
					ResultSet rset = stmt.executeQuery("select winner, mom, sixes, sixesWinner from matchresults where matchId='"+matchId+"'");

					while (rset.next()) {
						resultMap.put("matchId" , matchId);
						resultMap.put("winner" , rset.getString(1));
						resultMap.put("mom" , rset.getString(2));
						resultMap.put("sixes" , rset.getString(3));
						resultMap.put("sixesWinner" , rset.getString(4));
					}
					System.out.println("resultMap matchId "+resultMap.get("matchId"));
					System.out.println("resultMap winner "+resultMap.get("winner"));
					System.out.println("resultMap mom "+resultMap.get("mom"));
					System.out.println("resultMap sixes "+resultMap.get("sixes"));
					System.out.println("resultMap sixesWinner "+resultMap.get("sixesWinner"));
					
					
					String inParam = "General, RVA, Office";
					rset = stmt.executeQuery("select winner, mom, sixes, sixesWinner, userName, league from answers where matchId='"+matchId+"' and league in('General', 'RVA', 'Office')");

					while (rset.next()) {
						int winnerPoint = 0;
						int momPoint = 0;
						int sixesPoint = 0;
						int totalPoints = 0;
						System.out.println("winnerPoint "+winnerPoint);
						System.out.println("momPoint "+momPoint);
						System.out.println("sixesPoint "+sixesPoint);
						
						System.out.println("Res winnerPoint "+rset.getString(1));
						System.out.println("Res momPoint "+rset.getString(2));
						System.out.println("Res sixesPoint "+rset.getString(3));
						System.out.println("Res sixesWinner "+rset.getString(4));
						
						System.out.println("Map winnerPoint "+resultMap.get("winner"));
						System.out.println("Map momPoint "+resultMap.get("mom"));
						System.out.println("Map sixesPoint "+resultMap.get("sixes"));
						System.out.println("Map sixesWinner "+resultMap.get("sixesWinner"));
						
						if(rset.getString(1).equals((String)resultMap.get("winner"))){
							winnerPoint = 10;
						}
						if(rset.getString(2).equals((String)resultMap.get("mom"))){
							momPoint = 10;
						}
						if(rset.getString(3).equals((String)resultMap.get("sixes")) && 
								rset.getString(4).equals((String)resultMap.get("sixesWinner"))){
							sixesPoint = 10;
						}
						System.out.println("winnerPoint "+winnerPoint);
						System.out.println("momPoint "+momPoint);
						System.out.println("sixesPoint "+sixesPoint);
						System.out.println("=================== ");
						String query = "insert into points(matchId, winner, mom, sixes, total, voterId, league) values(?, ?, ?, ?, ?, ?, ?)";
						PreparedStatement pstmt = conn.prepareStatement(query); // create a statement
						pstmt.setString(1, matchId);
						pstmt.setString(2, String.valueOf(winnerPoint));
						pstmt.setString(3, String.valueOf(momPoint));
						pstmt.setString(4, String.valueOf(sixesPoint));
						pstmt.setString(5, String.valueOf(winnerPoint+momPoint+sixesPoint));
						pstmt.setString(6, rset.getString(5));
						pstmt.setString(7, rset.getString(6));
						
						pstmt.executeUpdate();
						pstmt.close();	
					}
					rset.close();
					stmt.close();
					conn.close();
					session.setAttribute("resposeText","Succesfully Submited");
				} catch (Exception e1) {
					e1.printStackTrace();
				} finally {
				}
				response.sendRedirect("results.jsp");
			%>
            </div><!-- content -->
            <div class="clearing">&nbsp;</div>   
        </div><!-- main -->
    </div><!-- page -->
    <div id="footer">
        <p>Copyright &copy; 2010, designed by <a href="http://www.kiranvenkat.in/">kiranvenkat.in</a></p>
    </div>
</body>
</html>
<%
   if ( session.getAttribute("resposeText") != null && !("").equals(session.getAttribute("resposeText")))
   { 
		session.setAttribute("resposeText", null);
   }
%>